RTC.ino

					
#include "RTC.h"
#include "Lampen.h"

void init_clock(my_clock_t* Clock)
{
  Clock->clock_pin_1 = A14; // Goes to IN1 on the Amplifier
  Clock->clock_pin_2 = A15; // Goes to IN2 on the Amplifier

  pinMode(Clock->clock_pin_1, OUTPUT);
  pinMode(Clock->clock_pin_2, OUTPUT); 

  Clock->tick_in_progress = false;
  Clock->stopped = true;
  
  Clock->ticks_to_do = 0;
  Clock->ticks_memory = 0;
  Clock->polarity = NEGATIVE;
  Clock->ticks_timer = 0;
  Clock->impulse_period = CLOCK_IMPULSE_PERIOD;//CLOCK_TICK_PERIOD;
  Clock->duty_cycle = CLOCK_IMP_DUTY_CYCLE;
  Clock->tick_period = (uint16_t)((long)Clock->impulse_period*(long)100/(long)Clock->duty_cycle);
}

void RTC_Set_Time(status_flags_t* Status, ts* time_struct)
{
  char Time_line[20];
  
  Serial1.println("ret_time");
  Serial.println("Time Request");
  delay(2);

  while(Status->debug_flag != 1)
    UART_Routine(Status, Time_line, true);    
  Status->debug_flag = 0;

  /******** PARSING OF THE RECEIVED STRING ***********/
  //Serial.print("Parsing the line:");
  //Serial.println(Time_line);

  if(!strcmp(Time_line, "NOWIFI"))  // Receiwed a signal about absence of WiFi connection. Do NOT set time then
  {
    DS3231_get(time_struct);
    Serial.println("From ESP-12F: No WiFi Connection! Time not synchronized");
  }
  else
  {
    char* ptr = strtok(Time_line, ":");
    char* ptr_temp = ptr;               // Pointer for implementing the atoi function
    uint8_t i = 0;
    while(ptr != NULL)
    {
      switch(i)
      {
        case 0:
        {
          time_struct->hour = atoi(ptr_temp);
        } break;
        case 1:
        {
          time_struct->min = atoi(ptr_temp);
        } break;
        case 2:
        {
          time_struct->sec = atoi(ptr_temp);
        } break;
        case 3:
        {
          time_struct->year = atoi(ptr_temp);
        } break;
        case 4:
        {
          time_struct->mon = atoi(ptr_temp);
        } break;
        case 5:
        {
          time_struct->mday = atoi(ptr_temp);
        } break;
      }
    
      ptr = strtok(NULL, ":");
      ptr_temp = ptr;
    
      i++;
    }
  /*********** WRITING OF THE FILLED STRUCTURE TO THE MODUL *****************/
    DS3231_set(*time_struct); 
    Serial.println("From ESP-12F: Time synchronized succesfully!");
  }
}

/*
 * Calculating of clock ticks to do. Returns amount of Minutes - difference between first and second parameter
 */
int16_t calculate_Ticks(ts* RTC_time, my_time_t* EEPROM_time)
{
  int16_t ticks;
  DS3231_get(RTC_time);
  
  Serial.println("RTC Time : " + (String)RTC_time->hour + ":" + (String)RTC_time->min);
  Serial.print("EEPROM Time: " + (String)EEPROM_time->hours + ":" + (String)EEPROM_time->minutes);
  
  ticks = (int16_t)abs((int16_t)(RTC_time->hour)%12 - (int16_t)(EEPROM_time->hours)%12)*60 + abs((int16_t)(RTC_time->min) - (int16_t)(EEPROM_time->minutes));

  // step from X.59 to X+1.00
  if((RTC_time->hour == ((EEPROM_time->hours+1)%24)) && (RTC_time->min == 0)) // if we have step to the next hour
  {
    ticks = 1;
  }
  
  EEPROM_time->hours = RTC_time->hour; // Temporary saving of the structure for next calculations
  EEPROM_time->minutes = RTC_time->min;
  
  return ticks;
}

void do_impulse(my_clock_t* Clock)
{
  switch(Clock->polarity)
  {
    case NEGATIVE: // than switch to POSITIVE
    {
      digitalWrite(Clock->clock_pin_1, HIGH);
      digitalWrite(Clock->clock_pin_2, LOW);
      Clock->polarity = POSITIVE;
    } break;
    case POSITIVE: // than switch to NEGATIVE
    {
      digitalWrite(Clock->clock_pin_1, LOW);
      digitalWrite(Clock->clock_pin_2, HIGH);
      Clock->polarity = NEGATIVE;
    } break;
  }
}

void stop_clock(my_clock_t* Clock)
{
  digitalWrite(Clock->clock_pin_1, LOW);
  digitalWrite(Clock->clock_pin_2, LOW);
}